package com.multi.web.controller.dataservice;

import com.multi.common.annotation.DataSource;
import com.multi.common.annotation.Log;
import com.multi.common.core.controller.BaseController;
import com.multi.common.core.domain.AjaxResult;
import com.multi.common.core.page.TableDataInfo;
import com.multi.common.enums.BusinessType;
import com.multi.common.utils.poi.ExcelUtil;
import com.multi.source.domain.BkDataSourceInfo;
import com.multi.source.resp.BkDataSourceInfoResp;
import com.multi.source.service.DataSourceInfoService;
import com.multi.source.vo.DataSourceInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 数据配置类
 * @ClassName DataSourceController
 * @Author pzj
 * @Data 2024/11/19 10:12
 */
@RestController
@RequestMapping("source/data")
public class DataSourceController extends BaseController {

    @Autowired
    private DataSourceInfoService dataSourceInfoService;

    /**
     * 查询数据库资源配置信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DataSourceInfoVo dataSourceInfoVo)
    {
        startPage();
        List<BkDataSourceInfoResp> list = dataSourceInfoService.selectBkDataSourceInfoList(dataSourceInfoVo);
        return getDataTable(list);
    }

    /**
     * 导出数据库资源配置信息列表
     */
    @Log(title = "数据库资源配置信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataSourceInfoVo dataSourceInfoVo)
    {
        List<BkDataSourceInfoResp> list = dataSourceInfoService.selectBkDataSourceInfoList(dataSourceInfoVo);
        ExcelUtil<BkDataSourceInfoResp> util = new ExcelUtil<BkDataSourceInfoResp>(BkDataSourceInfoResp.class);
        util.exportExcel(response, list, "数据库资源配置信息数据");
    }

    /**
     * 获取数据库资源配置信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dataSourceInfoService.selectBkDataSourceInfoById(id));
    }

    /**
     * 新增数据库资源配置信息
     */

    /**
     * 修改数据库资源配置信息
     */
    @Log(title = "数据库资源配置信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BkDataSourceInfo bkDataSourceInfo)
    {
        return toAjax(dataSourceInfoService.updateBkDataSourceInfo(bkDataSourceInfo));
    }

    /**
     * 删除数据库资源配置信息
     */
    @Log(title = "数据库资源配置信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dataSourceInfoService.deleteBkDataSourceInfoByIds(ids));
    }


    @Log(title = "创建配置数据库配置",businessType = BusinessType.OTHER)
    @PostMapping("create")
    public AjaxResult createDataSource(@Validated @RequestBody DataSourceInfoVo dataSourceInfoVo){
        dataSourceInfoService.createDataSource(dataSourceInfoVo);
        return AjaxResult.success();
    }

    @Log(title = "查询数据库关联配置",businessType = BusinessType.OTHER)
    @GetMapping("selectDataSource/{tenantId}")
    public AjaxResult selectDataSource(@PathVariable Long tenantId){
        BkDataSourceInfo bkDataSourceInfo= dataSourceInfoService.selectDataSource(tenantId);
        return AjaxResult.success(bkDataSourceInfo);
    }
}
